package com.example.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Random;

public class RetrieveActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText et_newPassword;
    private EditText et_newPasswordAgain;
    private Button bt_code;
    private EditText et_code;
    private Button bt_retrieve;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrieve);
        et_newPassword = findViewById(R.id.et_newPassword);
        et_newPasswordAgain = findViewById(R.id.et_newPasswordAgain);
        bt_code = findViewById(R.id.bt_code);
        et_code = findViewById(R.id.et_code);
        bt_retrieve = findViewById(R.id.bt_retrieve);
        bt_code.setOnClickListener(this);
        bt_retrieve.setOnClickListener(this);
    }

    private String result = "";
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.bt_code) {
            Random random = new Random();
            for (int i = 0; i < 6; i++) {
                result += random.nextInt(10);
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("请记住验证码");
            Intent intent = getIntent();
            String phone = intent.getStringExtra("phone");
            String message = String.format("手机号%s，您的验证码为%s，请输入验证码~~", phone, result);
            builder.setMessage(message);
            builder.setPositiveButton("好的", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        } else if (view.getId() == R.id.bt_retrieve) {
            String fir = et_newPassword.getText().toString();
            String sec = et_newPasswordAgain.getText().toString();
            if (fir.length() < 6 || sec.length() < 6) {
                Toast.makeText(this, "请输入正确的密码", Toast.LENGTH_SHORT).show();
                return;
            }

            if (!fir.equals(sec)) {
                Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                return;
            }
            if (!et_code.getText().toString().equals(result)) {
                Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.putExtra("new_password", fir);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        }
    }
}